package com.conytech.conydishm;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class BottomMenu extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.bottommenu, container, false);

		View menu_btn1 = view.findViewById(R.id.menu1_btn);
		View menu_btn2 = view.findViewById(R.id.menu2_btn);
		View menu_btn3 = view.findViewById(R.id.menu3_btn);
		View menu_btn4 = view.findViewById(R.id.menu4_btn);
		View menu_btn5 = view.findViewById(R.id.menu5_btn);

		menu_btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnClick(v);
			}
		});
		menu_btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnClick(v);
			}
		});
		menu_btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnClick(v);
			}
		});
		menu_btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnClick(v);
			}
		});
		menu_btn5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnClick(v);
			}
		});


		return view;
	}

	//하단 메뉴 버튼 클릭
	public void mOnClick(View v) {

		if(v.getId() != R.id.menu1_btn) TabReset(R.id.menu1_btn);
		if(v.getId() != R.id.menu2_btn) TabReset(R.id.menu2_btn);
		if(v.getId() != R.id.menu3_btn) TabReset(R.id.menu3_btn);
		if(v.getId() != R.id.menu4_btn) TabReset(R.id.menu4_btn);
		if(v.getId() != R.id.menu5_btn) TabReset(R.id.menu5_btn);

		TextView currentOption = (TextView) getView().findViewById(v.getId());

		currentOption.setBackgroundColor(getResources().getColor(android.R.color.white));
		currentOption.setTextColor(getResources().getColor(android.R.color.black));
		//currentOption.requestFocus(optionId);
		currentOption.setFocusable(false);
		currentOption.setClickable(false);

		//레이아웃의 모든 뷰 삭제
		LinearLayout contentsLayout = (LinearLayout) getView().findViewById(R.id.container_content);
		LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if(contentsLayout != null){
			contentsLayout.removeAllViews();
		}

		switch (v.getId()){
			case R.id.menu1_btn:
				Log.d("conydishm", "메뉴1클릭");
				//inflate layout
				inflater2.inflate(R.layout.content1, contentsLayout, true);

			break;

			case R.id.menu2_btn:
				Log.d("conydishm", "메뉴2클릭");

				//inflate layout
				inflater2.inflate(R.layout.content2, contentsLayout, true);

				AsyncHttpClient client = new AsyncHttpClient();
				String url = "http://192.168.11.38/service/android/mcGetClientInfo.jsp";

				//파라메터
				RequestParams params = new RequestParams();
				params.put("action", "get_address");

				client.post(url, params, new AsyncHttpResponseHandler(){

					public void onStart() {
						//System.out.println("시작시작");
					}

					@Override
					public void onSuccess(String response) {
						xmlPasing(response);
					}

					@Override
					public void onFailure(Throwable error) {
						System.out.println("실패");
					}

				});
			break;

			case R.id.menu3_btn:
				Log.d("conydishm", "메뉴3클릭");
				//inflate layout
				inflater2.inflate(R.layout.content3, contentsLayout, true);

				//웹뷰 초기화
				WebView webview;
				webview = (WebView) getView().findViewById(R.id.webview);

				webview.setWebViewClient(new WebViewClient());
				webview.getSettings().setJavaScriptEnabled(true);
				webview.loadUrl("http://www.naver.com");

			break;

			case R.id.menu4_btn:
				Log.d("conydishm", "메뉴4클릭");

				//inflate layout
				inflater2.inflate(R.layout.content4, contentsLayout, true);

			break;

			case R.id.menu5_btn:
				Log.d("conydishm", "메뉴5클릭");

				Intent intent;
				intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://naver.com"));
				startActivity(intent);

			break;
		}
	}


	public void TabReset(int ViewId) {
		TextView currentOption = (TextView) getView().findViewById(ViewId);
		currentOption.setBackgroundColor(getResources().getColor(android.R.color.black));
		currentOption.setTextColor(getResources().getColor(android.R.color.white));
		//currentOption.requestFocus(optionId);
		currentOption.setFocusable(true);
		currentOption.setClickable(true);
	}

	//xml 파싱
	public void xmlPasing(String response) {
		//System.out.println(response);

		String tag_name, client_code, client_name, tel_no, img_yn;
		tag_name = "";
		client_code = "";
		client_name = "";
		tel_no = "";
		img_yn = "";

		int tagIdentifier = 0;

		ArrayList<ClientItem> client_items = new ArrayList<ClientItem>();

		try {
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();

			//parser.getAttributeValue(null, "value");		//속성값 가져올때
			parser.setInput(new ByteArrayInputStream(response.getBytes("UTF-8")), "UTF-8");
			parser.next();

			int parserEvent = parser.getEventType();
			while(parserEvent != XmlPullParser.END_DOCUMENT) {

				switch(parserEvent) {
					case XmlPullParser.START_DOCUMENT:
					break;

					case XmlPullParser.START_TAG:
						tag_name = parser.getName();

						if(tag_name.equals("client_code")) tagIdentifier = 1;
						else if(tag_name.equals("client_name")) tagIdentifier = 2;
						else if(tag_name.equals("tel_no")) tagIdentifier = 3;
						else if(tag_name.equals("img_yn")) tagIdentifier = 4;

					break;

					case XmlPullParser.END_TAG:
						tag_name = parser.getName();
						if(tag_name.equals("item")) {
							client_items.add(new ClientItem(client_code, client_name, tel_no, img_yn));
						}

					break;

					case XmlPullParser.TEXT:

						if(tagIdentifier == 1) client_code = parser.getText();
						else if(tagIdentifier == 2) client_name = parser.getText();
						else if(tagIdentifier == 3) tel_no = parser.getText();
						else if(tagIdentifier == 4) img_yn = parser.getText();


						tagIdentifier = 0;

					break;
				}

				parserEvent=parser.next();
			}

/*			// 1. pass context and data to the custom adapter
			MyAdapter adapter = new MyAdapter(this, client_items);
			// 2. Get ListView from activity_main.xml
			ListView listView = (ListView) findViewById(R.id.list1);

			//이미지 캐쉬 초기화
			adapter.imageLoader.clearCache();
			adapter.notifyDataSetChanged();

			// 3. setListAdapter
			listView.setAdapter(adapter);
*/
		} catch (Exception e) {
			e.printStackTrace();
		}

	}


}